package com.xuanyue.db.xuan;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xuanyue.db.xuan.core.orm.OrmUtils;
import com.xuanyue.db.xuan.core.table.expr.ExprValue;
import com.xuanyue.db.xuan.core.table.expr.ExprValueType;

public class T {
	private static String dataPath = "e:/tmp/dataPath";
	private static void deletePath(File path) {
		
		for(File s:path.listFiles()) {
			if(s.isFile()) {
				s.delete();
			}else {
				deletePath(s);
			}
		}
		path.delete();
		
	}
	private static void iniDB() {
		File m = new File(dataPath);
		if(m.exists()) {
			deletePath(m);
		}
		m.mkdirs();
		SeachContextV2.init(dataPath);
		
		String sql = "create table c_phone_t1 (c_phone list(uint(4),11),c_Operator uint(2),C_Utiid  uint(24),C_MarketPrice ufloat(32,2),C_ProId uint(6),C_CityId uint(10),C_IsMy boolean,C_IsYd boolean,C_IsEnable boolean,C_IsSuperGood boolean,C_IsHome boolean,C_IsNominate map(boolean,zhw,xxh,sjh,mix3to1,qg,test),C_IsSpecial map(boolean,zhw,xxh,sjh,mix3to1,qg,test),C_IsShelves uint(2),C_IsCostEffective  boolean),set source(80)";
		SeachContextV2.exeSql(sql);
	}
	
	public static void main(String[] args) throws Exception {
		iniDB();
		SeachContextV2.getTable("c_phone_t1" ).flush(1000000);;
		StringBuffer fx = new StringBuffer();
		StringBuffer vx = new StringBuffer();
		Field[] fs = Phone.class.getDeclaredFields();
		for(Field f:fs) {
			if(fx.length()>0) {
				fx.append(",");
				vx.append(",");
			}
			fx.append(f.getName());
			vx.append("?");
			vx.append(f.getName());
		}
		String sql = String.format("insert into c_phone_t1(%s) values ( %s )", fx.toString(),vx.toString());
		
		java.util.Random random = new java.util.Random();
		List<ExprValue> xl = new ArrayList<ExprValue>();
		Phone ph = new Phone();
		ph.setC_phone(new ArrayList<>());
		ph.setC_IsNominate(new HashMap<>());
		ph.setC_IsSpecial(new HashMap<>());
		long s=System.currentTimeMillis();
		for(int j=0;j<1000;j++) {
			xl.clear();
			long star=System.currentTimeMillis();
			for(int i=0;i<1000;i++) {
				ph.init(random);
				xl.add( OrmUtils.obj2ExprValue(ph));
			}
			System.out.println(String.format("make data time length %s ", System.currentTimeMillis()-star));
			star=System.currentTimeMillis();
			ExprValue p = new ExprValue();
			p.setType(ExprValueType.MAP);
			Map<String,ExprValue> info = new HashMap<>();
			info.put("isBatch", new ExprValue(ExprValueType.BOOLEAN,true) );
			info.put("values",  new ExprValue(ExprValueType.ARRAY,xl)  );
			
			SeachContextV2.exeSql(sql, new ExprValue(ExprValueType.MAP,info));
			System.out.println(String.format("insert into SUCCESS time length %s ", System.currentTimeMillis()-star));
		}
		System.out.println(System.currentTimeMillis()-s);
		SeachContextV2.getTable("c_phone_t1" ).save();
	}

}
